package com.cqvie.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cqvie.model.CourseSchedule;

@Repository("courseScheduleDao")
public class CourseScheduleDao {
	@Resource
	private SessionFactory sessionFactory;
	
	//根据教师ID来获取课程安排表
	@SuppressWarnings("unchecked")
	public List<CourseSchedule> SelectCourseID(int i){
		String hql="from CourseSchedule cs where cs.teacherID=?";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setInteger(0, i);
		List<CourseSchedule> listCourseID =query.list();
		if(listCourseID.size()>=1){
			return listCourseID;
		}else
			return null;
	}
	
	//根据学生ID来修改课程安排表的StuID(交接权限)
	public boolean updateCSstuID(String stuID,int t_id,int c_id){
		int temp;
		String hql="update CourseSchedule cs set cs.stuID=? where cs.teacherID=? and cs.courseID=?";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setString(0, stuID);
		query.setInteger(1, t_id);
		query.setInteger(2, c_id);
		temp=query.executeUpdate();
		if(temp>=1){
			return true;
		}else
			return false;
	}
	
	//根据学生ID来查询课程安排表(检查权限)
	public boolean isExistStuID(int stuID){
		String stu_ID= Integer.toString(stuID);
		String hql="from CourseSchedule cs  where cs.stuID=?";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setString(0, stu_ID);
		if(query.list().size()>=1){
			return true;
		}else
			return false;
	}
	
	public	CourseSchedule  GetCourseSchedule(String id){
		Session session=sessionFactory.getCurrentSession();
		CourseSchedule courseSchedule=(CourseSchedule)session.get(CourseSchedule.class, id);
		return courseSchedule;
	}
	
	//添加课程安排变信息
	public  void addCS(CourseSchedule cs){
		Session session = sessionFactory.getCurrentSession();
		session.save(cs);
	}
	
	//修改课程安排表
	public boolean updateCS(String ID,int t_id,int c_id){
		String hql="update CourseSchedule cs set cs.courseID=? ,cs.teacherID=? where cs.ID=?";
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);
		query.setInteger(0, c_id);
		query.setInteger(1, t_id);
		query.setString(2, ID);
		int temp=query.executeUpdate();
		if(temp>=1){
			return true;
		}else
			return false;
	}
}
